/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package WebContainer;

import javax.servlet.http.HttpServlet;
import Shop.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ThanhTam
 */
public class ListTypeServlet extends HttpServlet {

    private Connection conn;
    private void Connection() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/digital1", "root", "");
        } catch (Exception ex) {
            System.err.println("Error loading database driver ");
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Connection();
            ListType lsType = new ListType(conn);
            ArrayList<TypeProduct> k = lsType.getListType();
            req.setAttribute("lstType", k);
            RequestDispatcher view = req.getRequestDispatcher("index.jsp");
            view.forward(req, resp);
        } catch (SQLException ex) {
            Logger.getLogger(ListTypeServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

}
